﻿using System.Collections.Generic;
using Engine;
using Engine.Graphics;

namespace Game
{
    public class tjgsBlock : CrossBlock
    {
        public const int Index = 357;

        public override BlockDebrisParticleSystem CreateDebrisParticleSystem(SubsystemTerrain subsystemTerrain, Vector3 position, int value, float strength)
        {
            return new BlockDebrisParticleSystem(subsystemTerrain, position, strength, DestructionDebrisScale, BlockColorsMap.GrassColorsMap.Lookup(subsystemTerrain.Terrain, Terrain.ToCell(position.X), Terrain.ToCell(position.Y), Terrain.ToCell(position.Z)), GetFaceTextureSlot(4, value));
        }

        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            BlocksManager.DrawFlatBlock(primitivesRenderer, value, size, ref matrix, null, color * BlockColorsMap.GrassColorsMap.Lookup(environmentData.Temperature, environmentData.Humidity), isEmissive: false, environmentData);
        }

        public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
        {
            generator.GenerateCrossfaceVertices(this, value, x, y, z, BlockColorsMap.GrassColorsMap.Lookup(generator.Terrain, x, y, z), GetFaceTextureSlot(0, value), geometry.SubsetAlphaTest);
        }

        public override void GetDropValues(SubsystemTerrain subsystemTerrain, int oldValue, int newValue, int toolLevel, List<BlockDropValue> dropValues, out bool showDebris)
        {
            if (!GetIsSmall(Terrain.ExtractData(oldValue)))
            {
                int num = Random.Int(1, 3);
                BlockDropValue blockDropValue;
                BlockDropValue blockDropValue2;
                BlockDropValue item;
                for (int i = 0; i < num; i++)
                {
                    blockDropValue = default(BlockDropValue);
                    blockDropValue.Value = Terrain.MakeBlockValue(358, 0, 0);
                    blockDropValue.Count = 1;
                    blockDropValue2 = blockDropValue;
                    item = blockDropValue2;
                    dropValues.Add(item);
                }

                blockDropValue = default(BlockDropValue);
                blockDropValue.Value = Terrain.MakeBlockValue(358);
                blockDropValue.Count = 1;
                blockDropValue2 = blockDropValue;
                item = blockDropValue2;
                dropValues.Add(item);
            }

            showDebris = true;
        }

        public override int GetFaceTextureSlot(int face, int value)
        {
            if (!GetIsSmall(Terrain.ExtractData(value)))
            {
                return 112;
            }

            return 112;
        }

        public static bool GetIsSmall(int data)
        {
            return (data & 8) > 0;
        }

        public override int GetShadowStrength(int value)
        {
            if (!GetIsSmall(Terrain.ExtractData(value)))
            {
                return DefaultShadowStrength;
            }

            return DefaultShadowStrength / 2;
        }

        public static int SetIsSmall(int data, bool isSmall)
        {
            if (!isSmall)
            {
                return data & -9;
            }

            return data | 8;
        }
    }
}